package java课题设计.功能类.修改学生或者删除学生;
 
import java.io.*;
 
public class DelStudent {
    public static void removeLine(String filename, String targetLine) throws IOException {
        File targetFile = new File(filename);
        File tmpFile = new File("D:\\aaa\\数据\\tmp.txt");
        BufferedReader reader = new BufferedReader(new FileReader(targetFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tmpFile));
        String currentLine;
        boolean found = false;
        while ((currentLine = reader.readLine()) != null) {
            if (!currentLine.trim().equals(targetLine)) { // 如果当前行不是目标行，则写入到临时文件中
                writer.write(currentLine);
                writer.newLine();
            } else {
                found = true;
            }
        }
        if (!found) { // 没有找到目标行，抛出异常
            throw new IllegalArgumentException("Target line not found");
        }
        reader.close();
        writer.close();
        if (targetFile.delete()) { // 删除原文件
            if (!tmpFile.renameTo(targetFile)) { // 重命名临时文件
                throw new IOException("Failed to rename file");
            }
        } else {
            throw new IOException("Failed to delete file");
        }
    }
    }
